load("@fmeum_rules_jni//jni:defs.bzl", "java_jni_library")
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("//bazel:kotlin.bzl", "ktlint")
load("//src/jmh/java/com/code_intelligence/jazzer:jmh.bzl", "JMH_TEST_ARGS")

package(default_testonly = True)

java_binary(
    name = "CoverageInstrumentationBenchmark",
    main_class = "org.openjdk.jmh.Main",
    runtime_deps = [
        ":coverage_instrumentation_benchmark",
    ],
)

java_test(
    name = "CoverageInstrumentationBenchmarkTest",
    args = JMH_TEST_ARGS,
    jvm_flags = [
        "-XX:CompileCommand=print,*CoverageMap.recordCoverage",
    ],
    main_class = "org.openjdk.jmh.Main",
    # Directly invoke JMH's main without using a testrunner.
    use_testrunner = False,
    runtime_deps = [
        ":coverage_instrumentation_benchmark",
    ],
)

java_library(
    name = "coverage_instrumentation_benchmark",
    srcs = ["CoverageInstrumentationBenchmark.java"],
    plugins = ["//src/jmh/java/com/code_intelligence/jazzer:JmhGeneratorAnnotationProcessor"],
    runtime_deps = [
        "@maven//:com_mikesamuel_json_sanitizer",
    ],
    deps = [
        ":kotlin_strategies",
        ":strategies",
        "//src/main/java/com/code_intelligence/jazzer/instrumentor",
        "@maven//:org_openjdk_jmh_jmh_core",
    ],
)

java_library(
    name = "strategies",
    srcs = [
        "DirectByteBuffer2CoverageMap.java",
        "DirectByteBufferCoverageMap.java",
        "Unsafe2CoverageMap.java",
        "UnsafeBranchfreeCoverageMap.java",
        "UnsafeCoverageMap.java",
        "UnsafeSimpleIncrementCoverageMap.java",
    ],
    deps = [
        "//src/main/java/com/code_intelligence/jazzer/instrumentor",
        "@jazzer_jacoco//:jacoco_internal",
        "@org_ow2_asm_asm//jar",
    ],
)

kt_jvm_library(
    name = "kotlin_strategies",
    srcs = ["DirectByteBufferStrategy.kt"],
    deps = [
        "//src/main/java/com/code_intelligence/jazzer/instrumentor",
        "@jazzer_jacoco//:jacoco_internal",
        "@org_ow2_asm_asm//jar",
    ],
)

java_binary(
    name = "EdgeCoverageInstrumentationBenchmark",
    main_class = "org.openjdk.jmh.Main",
    runtime_deps = [
        ":edge_coverage_instrumentation_benchmark",
    ],
)

java_test(
    name = "EdgeCoverageInstrumentationBenchmarkTest",
    args = JMH_TEST_ARGS,
    main_class = "org.openjdk.jmh.Main",
    # Directly invoke JMH's main without using a testrunner.
    use_testrunner = False,
    runtime_deps = [
        ":edge_coverage_instrumentation_benchmark",
    ],
)

java_jni_library(
    name = "edge_coverage_instrumentation_benchmark",
    srcs = [
        "EdgeCoverageInstrumentation.java",
        "EdgeCoverageTarget.java",
    ],
    native_libs = ["//src/main/native/com/code_intelligence/jazzer/driver:jazzer_driver"],
    plugins = ["//src/jmh/java/com/code_intelligence/jazzer:JmhGeneratorAnnotationProcessor"],
    deps = [
        "//src/main/java/com/code_intelligence/jazzer/instrumentor",
        "//src/main/java/com/code_intelligence/jazzer/runtime:coverage_map",
        "//src/test/java/com/code_intelligence/jazzer/instrumentor:patch_test_utils",
        "@maven//:org_openjdk_jmh_jmh_core",
    ],
)

ktlint()
